package com.ycp.erp.main.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.one.common.config.CPersisData;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.event.BusManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.model.event.PicEvent;
import com.one.common.utils.ImmersionUtils;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseFragment;
import com.one.common.view.base.BaseFragmentActivity;
import com.skateboard.zxinglib.CaptureActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.ycp.erp.R;
import com.ycp.erp.main.fragment.HomeFragment;
import com.ycp.erp.main.fragment.MineFragment;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

/**
 * Class Des:
 * Created by bjh on 2021/8/23.
 */
@Route(path = RouterPath.MAIN_PAGE)
public class MainActivity extends BaseFragmentActivity {
    @BindView(R.id.frame_content)
    FrameLayout frameContent;
    @BindView(R.id.ivHome)
    ImageView ivHome;
    @BindView(R.id.tvHome)
    TextView tvHome;
    @BindView(R.id.llHome)
    LinearLayout llHome;
    @BindView(R.id.ivMine)
    ImageView ivMine;
    @BindView(R.id.tvMine)
    TextView tvMine;
    @BindView(R.id.llMine)
    LinearLayout llMine;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    @BindView(R.id.vLine)
    View vLine;
    @BindView(R.id.ivScan)
    ImageView ivScan;

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initTitle() {
        super.initTitle();
        noHasTitle();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        super.initView();
        llHome.performClick();
        RxView.clicks(ivScan).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(
                    CAMERA)
                    .subscribe(aBoolean -> {
                        if (aBoolean) { // 在android 6.0之前会默认返回true
                            Intent intent=new Intent(MainActivity.this, CaptureActivity.class);
                            startActivityForResult(intent,1001);
                        } else {
                            // 未获取权限
                            Toaster.showShortToast("您没有授权相机权限，请在设置中打开授权");
                            finishPage();
                        }
                    });

        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1001 && resultCode== Activity.RESULT_OK)
        {
            String result=data.getStringExtra(CaptureActivity.KEY_DATA);
            Toast.makeText(this, "条码信息："+result, Toast.LENGTH_SHORT).show();
        }else if (resultCode == RESULT_OK) {
            //我的页面更换头像
            ArrayList<String> pathList =
                    data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
            if (pathList != null && pathList.size() > 0) {
                String path = pathList.get(0);
                if (StringUtils.isNotBlank(path)) {
                    BusManager.getBus().post(new PicEvent(path));
                }
            }
        }
    }
    @OnClick(R.id.llHome)
    public void changeToHome(View view) {
        tvHome.setTextColor(getResources().getColor(R.color.root_green));
        ivHome.setImageResource(R.mipmap.tab_hone_selection);
        tvMine.setTextColor(getResources().getColor(R.color.color_666666));
        ivMine.setImageResource(R.mipmap.tab_my_default);
        changePage(0);
        ImmersionUtils.setImmersionGray(this);
    }
    @OnClick(R.id.llMine)
    public void changeToMine(View view) {
        tvHome.setTextColor(getResources().getColor(R.color.color_666666));
        ivHome.setImageResource(R.mipmap.tab_hone_default);
        tvMine.setTextColor(getResources().getColor(R.color.root_green));
        ivMine.setImageResource(R.mipmap.tab_my_selection);
        ImmersionUtils.setImmersionWhite(this);
        changePage(1);
    }
    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected ArrayList<BaseFragment> getFragments() {
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new MineFragment());
        return fragments;
    }
    @Override
    protected int getContainerViewId() {
        return R.id.frame_content;
    }
}
